package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.powernode.domain.UserAddr;
import com.powernode.service.UserAddrService;
import com.powernode.utils.PowerAuthMenuUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("p/address")
@Api(tags = "收货地址接口")
public class UserAddressController {
    @Autowired
    private UserAddrService userAddrService;

    @GetMapping("/list")
    @ApiOperation("收货地址接口")
    public ResponseEntity<List<UserAddr>> getAddrList() {
        String wxUserId = PowerAuthMenuUtil.getWxUserId();
        List<UserAddr> list = userAddrService.list(new LambdaQueryWrapper<UserAddr>()
                .eq(UserAddr::getUserId, wxUserId));
        return ResponseEntity.ok(list);
    }

    @PostMapping()
    @ApiOperation("新增收货地址")
    public ResponseEntity<Void> addAddress(@RequestBody UserAddr userAddr) {
        String wxUserId = PowerAuthMenuUtil.getWxUserId();
        userAddrService.addAddress(wxUserId, userAddr);
        return ResponseEntity.ok().build();
    }


    @PutMapping("/defaultAddr/{addrId}")
    @ApiOperation("默认地址设置")
    public ResponseEntity<Void> defaultAddr(@PathVariable("addrId") Long addrId) {
        String wxUserId = PowerAuthMenuUtil.getWxUserId();
        userAddrService.defaultAddr(wxUserId, addrId);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/addrInfo/{addrId}")
    @ApiOperation("修改回显")
    public ResponseEntity<UserAddr> addrInfo(@PathVariable("addrId") Long addrId) {
        UserAddr userAddr = userAddrService.getById(addrId);
        return ResponseEntity.ok(userAddr);
    }

    @PostMapping("/loadUserAddrById")
    @ApiOperation("获取默认地址")
    UserAddr loadUserAddrById(@RequestBody String wxUserId) {
        return userAddrService.getOne(new LambdaQueryWrapper<UserAddr>()
                .eq(UserAddr::getUserId, wxUserId)
                .eq(UserAddr::getCommonAddr, 1));
    }
}
